package com.hmis.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hmis.domain.Prescription;
import com.hmis.service.PrescriptionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@Controller
public class PrescriptionDAO implements InitializingBean {

    public static PrescriptionService prescriptionService;
    public static PrescriptionDAO prescriptionDAO;
    @Autowired
    private PrescriptionService prescription;

    public List<Prescription> listByPatientName(String patientName) {
        LambdaQueryWrapper<Prescription> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Prescription::getPatientName, patientName);
        return prescriptionService.list(queryWrapper);

    }

    public Prescription getByPatientId(Long patientId) {
        LambdaQueryWrapper<Prescription> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Prescription::getPatientId, patientId).eq(Prescription::getIsDelete, 0);
        return prescriptionService.getOne(queryWrapper);
    }

    public Prescription save(Prescription prescription) {
        prescriptionService.save(prescription);
        Prescription res = getByPatientId(prescription.getPatientId());
        return res;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        prescriptionService = prescription;
        prescriptionDAO = this;
    }

    public void updatePrescription(Prescription prescriptionSt) {
        prescriptionService.updateById(prescriptionSt);
    }
}
